/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dankegongyu.lib.http.utils;

import android.util.Log;

import com.dankegongyu.lib.http.DKHttp;

import java.util.Locale;

public class DKHttpLogUtils {

    private static final String TAG = "DKHttp";

    private DKHttpLogUtils() {

    }

    public static void d(String msg) {
        log(Log.DEBUG, msg, null);
    }

    public static void d(String msg, Throwable tr) {
        log(Log.DEBUG, msg, tr);
    }

    public static void e(String msg) {
        log(Log.ERROR, msg, null);
    }

    public static void e(Exception e) {
        if (e != null) {
            log(Log.ERROR, e.getMessage(), e);
        }
    }

    public static void e(String msg, Throwable tr) {
        log(Log.ERROR, msg, tr);
    }

    private static void log(int logLevel, String msg, Throwable tr) {
        if (!DKHttp.isDebugMode()) {
            return;
        }
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = createTag(caller);
        switch (logLevel) {
            case Log.DEBUG:
                Log.d(tag, msg, tr);
                break;
            case Log.ERROR:
                Log.e(tag, msg, tr);
                break;
            default:
                Log.v(tag, msg, tr);
                break;
        }
    }

    private static StackTraceElement getCallerStackTraceElement() {
//        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
//        for (int i = 0; i < elements.length; i++) {
//            StackTraceElement e = elements[i];
//            Log.e(TAG, "----  the " + i + " element  ----");
//            Log.e(TAG, "toString: " + e.toString());
//            Log.e(TAG, "ClassName: " + e.getClassName());
//            Log.e(TAG, "FileName: " + e.getFileName());
//            Log.e(TAG, "LineNumber: " + e.getLineNumber());
//            Log.e(TAG, "MethodName: " + e.getMethodName());
//        }
        return Thread.currentThread().getStackTrace()[5];
    }

    private static String createTag(StackTraceElement caller) {
        String tag = "%s.%s(L:%d)";
        try {
            String callerClassName = caller.getClassName();
            callerClassName = callerClassName.substring(callerClassName.lastIndexOf(".") + 1);
            tag = String.format(Locale.getDefault(), tag, callerClassName, caller.getMethodName(), caller.getLineNumber());
        } catch (Exception e) {
            e.printStackTrace();
        }
        tag = TAG + ":" + tag;
        return tag;
    }
}
